package Number6;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
public class Elevator extends JFrame implements Runnable,ActionListener{
	private JButton f1;
	private JButton f2;
	private JButton f3;
	private JButton f4;
	private JPanel west;
	private Center center;
	private final int EWIDTH=300;
	private final int EHEIGHT=800;
	private BufferedImage ele;
	private Graphics bufferg;
	private Container container;
	private int ypos=0;
	private int end=0;
	public Elevator(String name)
	{
		super(name);
		//this.setResizable(false);
	//	this.setLayout(new FlowLayout());
		this.setSize(EWIDTH,EHEIGHT);
		this.setLocation(300,0);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		container =this.getContentPane();
		//west
		west=new JPanel();
		west.setLayout(new GridLayout(4,1));
		west.setBackground(Color.BLUE);
		container.add(west,BorderLayout.WEST);
		//buttons
		f2=new JButton("FLOOR2");
		f3=new JButton("FLOOR3");
		f4=new JButton("FLOOR4");
		f1=new JButton("FLOOR1");
		f1.addActionListener(this);
		f2.addActionListener(this);
		f3.addActionListener(this);
		f4.addActionListener(this);
		west.add(f4);
		west.add(f3);
		west.add(f2);
		west.add(f1);
		//center
		center =new Center();
		center.setBackground(Color.red);
		container.add(center);
		//Bufferimage
		ele=new BufferedImage(EWIDTH,EHEIGHT/4,BufferedImage.TYPE_INT_RGB);
		bufferg=ele.getGraphics();
		//new Thread
		new Thread(this).start();
	}
	public static void main(String [] args)
	{
		new Elevator("ELEVATOR").setVisible(true);
	}
	public void run()
	{
		while(true)
		{
			System.out.println(end);
			if(end>ypos)
			{
				ypos+=10;
				center.repaint();
			}
			if(end<ypos)
			{
				ypos-=10;
				center.repaint();
			}
			else
			{
				Thread.yield();
			}
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	//@Override
	public void actionPerformed(ActionEvent ae) {
		System.out.println("ACTIOM");
		if(ae.getSource()==f4)
		{
			System.out.print("hello");
			end=0;
		}
		if(ae.getSource()==f3)
		{
			end=EHEIGHT/4;
			System.out.println("f3 is clicked \n end ="+end);
		}
		if(ae.getSource()==f2)
		{
			end=EHEIGHT/4*2;
		}
		if(ae.getSource()==f1)
		{
			end=EHEIGHT/4*3;
		}
		
	}
	class Center extends JPanel
	{
		public void paintComponent(Graphics g)
		{
			
			g.setColor(Color.BLUE);
			g.fillRect(0,0,EWIDTH,EHEIGHT);
			
			
			bufferg.setColor(Color.BLACK);
			bufferg.fillRect(0, 0, EWIDTH, EHEIGHT/4);
			g.drawImage(ele,0, ypos-10, this);
			
		}

	}
}
